package com.grq.mis.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.grq.mis.entity.Permission;
import com.grq.mis.exception.BusinessException;
import com.grq.mis.exception.ResultEnum;
import com.grq.mis.model.view.PermissionView;
import com.grq.mis.service.PermissionService;
import com.grq.mis.validate.PermissionValidate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController extends BaseController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 权限树
     *
     * @param model
     * @return
     */
    @SaCheckPermission("permission:list")
    @GetMapping("/tree")
    public String tree(Model model) {

        List<PermissionView> tops = permissionService.createLambdaQuery().andEq(Permission::getParentId, 0).asc(Permission::getSort).select(PermissionView.class);
        model.addAttribute("tops", tops);

        return "permission/tree.html";
    }

    /**
     * 添加权限页面
     * @param model
     * @return
     */
    @SaCheckPermission("permission:add")
    @GetMapping("/addForm")
    public String addForm(Model model) {

        List<PermissionView> tops = permissionService.createLambdaQuery().andEq(Permission::getParentId, 0).asc(Permission::getSort).select(PermissionView.class);
        model.addAttribute("tops", tops);

        return "permission/add.html";
    }

    /**
     * 添加权限
     * @param permission
     * @param model
     * @return
     */
    @SaCheckPermission("permission:add")
    @PostMapping("/add")
    public String add(Permission permission, Model model) {


        PermissionValidate.add(permission);

        permissionService.addPermission(permission);

        return frameSuccess(model, ResultEnum.SUCCESS, "添加权限成功");
    }

    /**
     * 修改页面
     * @param id
     * @param model
     * @return
     */
    @SaCheckPermission("permission:update")
    @GetMapping("/updateForm")
    public String updateForm(@RequestParam Long id, Model model) {
        Permission permission = permissionService.single(id);
        if (permission == null) {
            throw new BusinessException(ResultEnum.PERMISSION_IS_NULL);
        }
        model.addAttribute("permission", permission);
        return "permission/update.html";
    }

    /**
     * 修改权限
     * @param permission
     * @param model
     * @return
     */
    @SaCheckPermission("permission:update")
    @PostMapping("/update")
    public String permissionUpdate(Permission permission, Model model) {

        PermissionValidate.update(permission);
        permissionService.updatePermission(permission);

        return frameSuccess(model, ResultEnum.SUCCESS, "修改权限成功");
    }

    /**
     * 删除权限
     * @param id
     * @param attr
     * @return
     */
    @SaCheckPermission("permission:delete")
    @GetMapping("/delete")
    public String permissionDelete(@RequestParam Long id, RedirectAttributes attr) {

        permissionService.deletePermission(id);

        return pageSuccess(attr, "/permission/tree", ResultEnum.SUCCESS, "删除权限成功");
    }

    /**
     * 图标示例
     * @return
     */
    @GetMapping("/icon")
    public String icon() {
        return "permission/icon.html";
    }

}
